package cht;

import static org.junit.Assert.assertTrue;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;


/**
 * Test to see that Narchi visits a corner in every round.
 * 
 * @author Terrence Chida
 *
 */
public class TestNarchiVisitCorner extends RobotTestBed {

  boolean visited = false;
  
  /**
   * Specifies that SittingDuck and Narchi are being matched up in this test.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,cht.Narchi";
  }

  
  /**
   * This test runs for 10 round.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  
  /**
   * After every turn, check to see if we are at (or at least near) a corner.
   * 
   * @param event Information on the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    // Get a snapshot of where Narchi is at the end of each turn.
    IRobotSnapshot narchi = event.getTurnSnapshot().getRobots()[1];
    double narchiX = narchi.getX();
    double narchiY = narchi.getY();
    final double ROBOT_WIDTH = 40.0;
    // Upper-right
    if ((narchiX >= (width - ROBOT_WIDTH)) && (narchiY >= (height - ROBOT_WIDTH))) {
      this.visited = true;
    }
    // Lower-right
    if ((narchiX >= (width - ROBOT_WIDTH)) && (narchiY <= ROBOT_WIDTH)) {
      this.visited = true;
    }
    // Upper-left
    if ((narchiX <= ROBOT_WIDTH) && (narchiY >= (height - ROBOT_WIDTH))) {
      this.visited = true;
    }
    // Lower-left
    if ((narchiX <= ROBOT_WIDTH) && (narchiY <= ROBOT_WIDTH)) {
      this.visited = true;
    }
  } // end onTurnEnded()
  
  
  /**
   * At the end of every round, test to see that a corner was visited.
   * 
   * @param event Information on the current state of the battle.
   */
  @Override
  public void onRoundEnded(RoundEndedEvent event) {
    assertTrue("No corners visited.", this.visited);
  }

} // end TestNarchiVisitCorner
